package com.tqjc.system.C24012.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.system.C24012.common.entity.SysFaultRecordInfoEntity;
import com.tqjc.system.C24012.common.entity.VO.FaultRecordInfoVO;
import com.tqjc.system.C24012.common.entity.VO.StatDataVO;
import com.tqjc.system.C24012.core.service.SysFaultRecordInfoService;
import com.tqjc.system.C24012.core.wrapper.FaultRecordWrapper;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.DateUtils;
import com.tqjc.system.core.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: 86195
 * @CreateTime: 2024-07-16  11:22
 * @Description: TODO
 * @Version: 1.0
 */

@Controller
@RequestMapping("/faultRecord")
@Slf4j
public class FaultRecordController extends BaseController {


    @Resource
    SysFaultRecordInfoService faultRecordService;

    @RequestMapping("/index")
    public String index() {
        return "C24012/fault/faultRecord.html";
    }


    @RequestMapping("/getStatData")
    @ResponseBody
    public AjaxJson getStatData(FaultRecordInfoVO faultRecordInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        Long id = faultRecordInfoVO.getId();
        try {
            log.info("错误信息统计数据接口开始");
            initDate(faultRecordInfoVO);
            List<StatDataVO> list = faultRecordService.getStatData(faultRecordInfoVO);
            log.info("错误信息统计数据接口结束");
            ajaxJson.setObj(list);
        } catch (Exception e){
            log.error("错误信息统计数据接口出现异常，id:{}",id,e);
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("错误信息统计数据接口出现异常");

        }
        return ajaxJson;
    }


    @RequestMapping("/listInfo")
    public void listInfo(FaultRecordInfoVO faultRecordInfoVO, PageInfo<SysFaultRecordInfoEntity> page, HttpServletResponse response){
        log.info("查询错误信息接口开始");
        initDate(faultRecordInfoVO);
        IPage<SysFaultRecordInfoEntity> lstResult = faultRecordService.getListByPage(page.getPageEntity(),faultRecordInfoVO);
        ResponseUtil.write(response, new FaultRecordWrapper(lstResult).wrap());
        log.info("查询错误信息接口结束");

    }


    private void initDate(FaultRecordInfoVO faultRecordInfoVO){
        if(StringUtils.isNotEmpty(faultRecordInfoVO.getHappenEndDateStr())){
            String happenEndDateStr = faultRecordInfoVO.getHappenEndDateStr() + " 23:59:59";
            faultRecordInfoVO.setHappenEndDate(DateUtils.getLocalDateTime(happenEndDateStr));
        }
        if(StringUtils.isNotEmpty(faultRecordInfoVO.getHappenStartDateStr())){
            String happenStartDateStr = faultRecordInfoVO.getHappenStartDateStr() + " 00:00:00";

            faultRecordInfoVO.setHappenStartDate(DateUtils.getLocalDateTime(happenStartDateStr));

        }

    }


}
